package org.csu.secondhand.api.controller.fronted;

import org.csu.secondhand.api.entity.Likes;
import org.csu.secondhand.api.entity.Posting;
import org.csu.secondhand.api.service.LikesService;
import org.csu.secondhand.api.service.PostingService;
import org.csu.secondhand.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/likes")
public class LikesController {
    @Autowired
    LikesService likesService;
    @Autowired
    PostingService postingService;

    @PostMapping("/doLike")
    @ResponseBody
    public Map<Object, Object> doLike(String username, long posting) {
        Map<Object, Object> map = new HashMap<>();
        Likes likes = likesService.doLike(new Likes(0, username, posting, TimeUtil.getTime()));
        postingService.likesPlus(posting);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", likes);
        return map;
    }

    @PostMapping("dontLike")
    @ResponseBody
    public Map<Object, Object> dontLike(String username, long posting) {
        Map<Object, Object> map = new HashMap<>();
        Likes likes = likesService.dontLike(username, posting);
        postingService.likesMinus(posting);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", likes);
        return map;
    }

    @GetMapping("doILikeThem")
    @ResponseBody
    public Map<Object, Object> doILikeThem(int type, String username) {
        Map<Object, Object> map = new HashMap<>();
        List<Posting> postingList = new ArrayList<>();
        if(type == 0) {
            postingList = postingService.getAllPosting();
        }
        if(type == 1) {
            postingList = postingService.getMyPosting(username);
        }
        if(type == 2) {
            postingList = postingService.getThoseILike(username);
        }
        if(type == 3) {
            postingList = postingService.getThoseIComment(username);
        }
        List<Integer> list = new ArrayList<>();
        for(int i=0; i<postingList.size(); i++) {
            list.add(likesService.doILikeIt(username, postingList.get(i).getId()));
        }
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", list);
        return map;
    }

    @GetMapping("doILikeIt")
    @ResponseBody
    public Map<Object, Object> doILikeIt(String username, long posting) {
        Map<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", likesService.doILikeIt(username, posting));
        return map;
    }
}
